<?php

namespace app\Modules\Api\controllers;



use Yii;
use app\Modules\Api\models\Saleorder;
use app\Modules\Api\models\Auction;
use app\Modules\Api\models\Addr;
use app\filters\Json;
use yii\data\Pagination;
use app\helpers\Request;

/**
 * Class OrderController
 * @package app\Modules\Api\controllers
 */
class OrderController extends ControllerBase
{
    public function actionIndex()
    {
        $user_id = Request::strictpost('user_id','缺少用户id');

        $query = Saleorder::find()->where(['user_id'=>$user_id]);

        $type = Yii::$app->request->post('type',0);

        $seeAuction = false;
        switch ($type){//查看类型
            //全部
            case 0:break;
            //我在拍
            case 1:$query->andWhere(['state'=>Saleorder::STEAT_PATTING]);break;
            //我拍中
            case 2:
                $query->andWhere(['not in','state',[Saleorder::STEAT_PATTING,Saleorder::STEAT_NOT_PATTED,Saleorder::STEAT_PATTED]]);
                break;
            //未拍中
            case 3:
                $query->andWhere(['state'=>Saleorder::STEAT_NOT_PATTED]);
                $seeAuction = true;
                break;
            //待付款
            case 4:
                $query->andWhere(['state'=>Saleorder::STEAT_PATTED]);
                break;
        }

        $count = $query->count();

        $pagination = new Pagination([
            'totalCount' => $count,
            'pageSize'=>Yii::$app->request->post('pageSize',6),
            'page'=>Yii::$app->request->post('page'),
        ]);


        $orderObjects = $query
            ->orderBy(['updated_at'=>SORT_DESC])
            ->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();

        $orders = [];

        foreach($orderObjects as $key=>$object){
            /** @var  $object Saleorder */
            $orders[$key] = $object->toArray([]);
            if($seeAuction||$object->state==Saleorder::STEAT_NOT_PATTED){
                $auction = Auction::findOne($object->auction_id);
                $orders[$key]['auction'] = $auction?$auction->toArray(['market_val','price','nickname']):new \stdClass();
                $orders[$key]['return_value'] = strval($object->num*$auction->return_rate*$auction->fee);
            }
        }

        $data = [
            'orders'=>$orders,
            'order_count'=>$count,
        ];

        return Json::success($data);
    }

    /**
     * @param $order_id
     * @return array
     */
    public function actionPayshow($order_id)
    {
        //$order_id = Request::strictpost('order_id','缺少订单id');
        $order = Saleorder::findOne($order_id);
        if(!$order)
            return Json::error('订单不存在');
        $user_id = $order->user_id;
        $addr = Addr::find()->where(['user_id'=>$user_id])->orderBy(['state'=>SORT_DESC,'addrid'=>SORT_DESC])->one();
        $addr = $addr?$addr:new \stdClass();
        $data = ['addr'=>$addr,'order'=>$order];
        return Json::success($data);
    }

    public function actionReceive()
    {
        $order_id = Request::strictpost('order_id','缺少订单id');
        $order = Saleorder::findOne($order_id);
        if(!$order)
            return Json::error('订单不存在');
        if(!$order->canChange(Saleorder::STEAT_RECEIVED))
            return Json::error('当前订单不是发货订单');
        $order->state = Saleorder::STEAT_RECEIVED;
        $order->save(false);
        return Json::success();
    }
}